package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.dao.ChainBrandDAO;
import com.junzhiit.nengji.model.ChainBrand;

@Repository
public class ChainBrandDAOImpl extends AbstractBaseDAO<ChainBrand> implements ChainBrandDAO{

	@Override
	public List<ChainBrand> findAll(int pageNumber, int maxPageRow, ChainBrand chainBrand) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from `chain_brand` where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());

		if (chainBrand != null) {
			if (StringUtils.isNotBlank(chainBrand.getChainId())) {
				sql.append(" and chain_id = ?");
				params.setString(chainBrand.getChainId());
			}
			if (StringUtils.isNotBlank(chainBrand.getName())) {
				sql.append(" and name like ?");
				params.setString("%" + chainBrand.getName() + "%");
			}
		}

		return queryForPagingList(sql.toString(), ChainBrand.class, params, pageNumber, maxPageRow);
	}

	@Override
	public Integer getTotalCount(ChainBrand chainBrand) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from `chain_brand` where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());

		if (chainBrand != null) {
			if (StringUtils.isNotBlank(chainBrand.getChainId())) {
				sql.append(" and chain_id = ?");
				params.setString(chainBrand.getChainId());
			}
			if (StringUtils.isNotBlank(chainBrand.getName())) {
				sql.append(" and name like ?");
				params.setString("%" + chainBrand.getName() + "%");
			}
		}

		return queryForInt(sql.toString(), params);
	}

	@Override
	public Integer getBrandExist(ChainBrand chainBrand) {
		StatementParameter params = new StatementParameter();
		StringBuffer sql = new StringBuffer();

		sql.append("select count(*) from `chain_brand` where is_delete=?");
		params.setInt(IsDelete.NO.toIntValue());
		
		if (chainBrand != null) {
			if (StringUtils.isNotBlank(chainBrand.getChainId())){
				sql.append(" and chain_id=?");
				params.setString(chainBrand.getChainId());
			}
			if (StringUtils.isNotBlank(chainBrand.getName())) {
				sql.append(" and name=?");
				params.setString(chainBrand.getName());
			}
		}

		return queryForInt(sql.toString(), params);
	}

	@Override
	public String getMaxCode() {
		String sql = "select max(code) from `brand`";

		return queryForString(sql.toString());
	}

	@Override
	public List<ChainBrand> findAlls(String chainId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select id,name from `chain_brand` where is_delete=?");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(chainId)){
			sql.append(" and chain_id=?");
			param.setString(chainId);
		}

		return queryForList(sql.toString(), ChainBrand.class, param);
	}
	
}
